<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<form style="margin-top: 15px" id="searchFrm" method="post" class="layui-form  layui-form-pane" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline">
                <input type="text" name="email" id="email" autocomplete="off"  class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">城市</label>
            <div class="layui-input-inline">
                <input type="text" name="city" id="city" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="doSearch">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>

<table class="layui-hide" id="test" lay-filter="test">
</table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button  class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 添加和修改账单的弹出层开始 -->
<div id="addOrUpdateBillDiv" style="display: none;margin: 10px">
    <form id="dataFrm" method="post" class="layui-form  layui-form-pane" lay-filter="dataFrm">
        <!-- 隐藏域，保存当前账单的ID -->
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" placeholder="请输入用户名" lay-verify="required" lay-reqText="请输入用户名"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" placeholder="请输入邮箱" lay-verify="required" lay-reqText="请输入邮箱"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">积分</label>
            <div class="layui-input-inline">
                <input type="text" name="experience"   lay-verify="number" placeholder="请输入积分"  lay-reqText="请输入账单金额"   autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-inline">
                <input type="text" name="sex"  placeholder="请输入性别"  lay-reqText="请输入账性别"   autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">登入次数</label>
            <div class="layui-input-inline">
                <input type="text" name="logins"   lay-verify="number" placeholder="请输入登入次数"  lay-reqText="请输入登入次数"   autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">加入时间</label>
            <div class="layui-input-block">
                <input type="text" name="jointime" id="jointime"  lay-reqText="请输入加入时间"   placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">城市</label>
            <div class="layui-input-inline">
                <input type="text" name="city"   placeholder="请输入城市"  lay-reqText="请输入城市"   autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">签名</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入签名" name="sign" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
</div>
<!-- 添加和修改账单的弹出层结束-->

<script th:src="@{/layui/layui.js}" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use(['form','table','layer','jquery','laydate'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var laydate = layui.laydate;


        //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
        //详见：https://www.layui.com/doc/modules/table.html#totalRow
        var tableIns = table.render({
            elem: '#test'
            ,url:'/listJson'
            ,toolbar: '#toolbarDemo'
            ,title: '用户数据表'
            ,totalRow: true
            ,cols: [
                [
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
                ,{field:'username', title:'用户名', width:120, edit: 'text'}
                ,{field:'email', title:'邮箱', width:150, edit: 'text', templet: function(res){
                        return '<em>'+ res.email +'</em>'
                    }}
                ,{field:'experience', title:'积分', width:80, sort: true, totalRow: true}
                ,{field:'sex', title:'性别', width:80, edit: 'text', sort: true}
                ,{field:'logins', title:'登入次数', width:100, sort: true, totalRow: true}
                ,{field:'sign', title:'签名'}
                ,{field:'city', title:'城市', width:100}
                ,{field:'jointime', title:'加入时间', width:120}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]
            ]
            ,page: true
            ,id:'orderTable'
        });

        var url;//提交地址
        var mainIndex;//窗口索引

        //监听搜索按钮提交事件
        form.on("submit(doSearch)",function (data) {
            $.post(url,data.field,function(result){

                console.log(data);
                var email = $("#eamil").val();
                var city = $("#city").val();
                    //刷新数据表格
                    tableIns.reload('orderTable', {
                        where: {
                            email: email,
                            city: city
                        },
                        //查询条件
                        page: {
                            curr: 1
                        }
                    });
                },"json");
            return false;
        });





        //
        // console.log(data);
        //     var email = $("#eamil").val();
        //     var city = $("#city").val();
        //     tableIns.reload('orderTable',{
        //         where: {
        //             email:email,
        //             city:city
        //         },
        //         //查询条件
        //         page:{
        //             curr:1
        //         }
        //     });
        //     //禁止页面刷新
        //     return false;
        // });


        //监听双击事件
        table.on('rowDouble(test)', function(data){
            mainIndex =layer.open({
                type:1,//弹出层类型
                title:"详细内容",
                area: ['800px', '600px'],
                content: $('#addOrUpdateBillDiv'),//引用的窗口代码
                btn:'关闭全部',
                success:function () {
                    // 清空表单数据
                    form.val("dataFrm",data);
                    // url = "/add";
                }
            });
        });

        //工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                //添加
                case 'add':
                    openAddWindow();
                    break;
            };
        });

        //监听表格行工具栏事件
        table.on("tool(test)",function (obj) {
            console.log(obj)
            switch (obj.event) {
                //编辑
                case 'edit':
                    openUpdateWindow(obj.data);//回显数据
                    break;
                //删除
                case 'del':
                    deleteById(obj.data);
                    break;
            }
        });


        function openAddWindow() {
            mainIndex =layer.open({
                type:1,//弹出层类型
                title:"添加账单",
                area: ['800px', '600px'],
                content: $('#addOrUpdateBillDiv'),//引用的窗口代码
                success:function () {
                    // 清空表单数据
                    $("#dataFrm")[0].reset();//JavaScript中的方法
                    url = "/add";
                }
            });
        }

        // 监听表单提交事件
        form.on("submit(doSubmit)",function (data) {
            $.post(url,data.field,function(result){
                if(result.success){
                    layer.alert(result.message,{icon:1});
                    //关闭窗口
                    layer.close(mainIndex);
                    //刷新数据表格
                    tableIns.reload();
                }else{
                    layer.alert(result.message,{icon:2});
                }
            },"json");
            return false;
        });

        /**
         * 打开编辑窗口
         */
        function openUpdateWindow(data) {
            mainIndex =layer.open({
                type:1,//弹出层类型
                title:"修改账单",
                area: ['800px', '600px'],
                content:$("#addOrUpdateBillDiv"),//引用的窗口代码
                success:function () {
                    // 表单数据回显
                    form.val("dataFrm",data);
                    //修改请求
                    url = "/update";
                }
            });
        }

        function deleteById(data) {
            //提示用户确认是否删除
            layer.confirm("确定要删除[<font color='red'>"+data.title+"</font>]账单吗？",{icon:3,title:"提示"},function (index) {
                //发送ajax请求
                $.post("/del",{"id":data.id},function(result){
                    if(result.success){
                        layer.alert(result.message,{icon:1});
                        //刷新数据表格
                        tableIns.reload();
                    }else{
                        layer.alert(result.message,{icon:2});
                    }
                },"json");

                //关闭提示框
                layer.close(index);
            });
        }

    });
</script>

</body>
</html>